//
//  ChangePwdViewController.m
//  HTP
//
//  Created by 陈钱钿 on 15/11/4.
//  Copyright © 2015年 nit. All rights reserved.
//

#import "ChangePwdViewController.h"
#import "ProfileModel.h"
#import "UserVM.h"
#import "SettingMainViewController.h"
#define kBorder 20
#define cellHeight 37
@interface ChangePwdViewController ()

@property (nonatomic, retain) UITextField *firstPwd;
@property (nonatomic, retain) UITextField *confirmPwd;
@property (nonatomic, retain) ProfileModel *profileModel;
@end

@implementation ChangePwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addCustomTitleWithTitle:@"密码修改"];
    self.view.backgroundColor = RGBCOLOR(233.0, 233.0, 233.0, 1);
    [self addCustomBackBase];
    [self initUI];
}
//绘制UI
- (void)initUI {
    UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0, kBorder, ScreenWidth, cellHeight*2)];
    newView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:newView];
    
    self.firstPwd = [[UITextField alloc] initWithFrame:CGRectMake(kBorder, 0, ScreenWidth, cellHeight)];
    self.firstPwd.placeholder = @"请输入新密码";
    [self.firstPwd setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
    self.firstPwd.secureTextEntry = YES;
    [newView addSubview:self.firstPwd];
    UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(kBorder, CGRectGetMaxY(self.firstPwd.frame), ScreenWidth-2*kBorder, 1)];
    line.image = [UIImage imageNamed:@"settingline"];
    [newView addSubview:line];
    self.confirmPwd = [[UITextField alloc] initWithFrame:CGRectMake(kBorder, CGRectGetMaxY(line.frame), ScreenWidth, cellHeight)];
    self.confirmPwd.placeholder = @"再次输入密码";
    [self.confirmPwd setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
    self.confirmPwd.secureTextEntry = YES;
    [newView addSubview:self.confirmPwd];
    
    
    UIButton *confirmBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(newView.frame)+kBorder, ScreenWidth, cellHeight)];
    confirmBtn.titleLabel.font = [UIFont systemFontOfSize:16.0];
    [confirmBtn setBackgroundColor:[UIColor whiteColor]];
    [confirmBtn setTitle:@"确认" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [confirmBtn addTarget:self action:@selector(changePwd) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:confirmBtn];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

//TODO:更改密码
- (void)changePwd{
    if (![_firstPwd.text isEqualToString:_confirmPwd.text]) {
        [self showSVProgressFailed:@"输入密码不一致"];
    }else if([_firstPwd.text isEqualToString:@""] || [_confirmPwd.text isEqualToString:@""]){
        [self showSVProgressFailed:@"不能为空"];
    }else {
        _profileModel = [ProfileModel mj_objectWithKeyValues:[USERCONTENT getDefaultUser]];
        UserVM *userVM = [[UserVM alloc] init];
        [userVM setBlockWithReturnBlock:^(id returnValue) {
            NSString *result = returnValue[INTERFACE_RESULT];
            DLog(@"change-------%@",result);
            if([result isEqualToString:@"Success"]){
                [self showSVProgressSuccess:@"修改成功"];
                self.profileModel.userPwd = _confirmPwd.text;
                //更新缓存
                NSDictionary *userDic = [[NSDictionary alloc] initWithDictionary:self.profileModel.mj_keyValues];
                [USERCONTENT setUserBean:userDic];
                [[NSNotificationCenter defaultCenter] postNotificationName:CHANGEPROFILE object:nil];
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [self showSVProgressFailed:@"修改失败"];
            }
        } WithErrorBlock:^(id errorCode) {
            [self showSVProgressFailed:@"修改失败"];
        } WithFailureBlock:^{
            [self showSVProgressFailed:INTERFACE_ERROR_CHIN];
        }];
        [self showSvprogress:@""];
        [userVM updateUserPwd:[NSString stringWithFormat:@"%@",_profileModel.userId] password:_confirmPwd.text oldPassword:_profileModel.userPwd];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
